﻿CREATE PROCEDURE [dbo].[proc_Finance_RunningAccount_UpdateOrder_G_WK]
	(	
		@Id VARCHAR(1000),
		@OrderId nvarchar(50),
		@PayId int
	)
AS
BEGIN
	BEGIN TRANSACTION--开始事务
DECLARE @errorSun INT --定义错误计数器
SET @errorSun=0 --没错为0
   
  -------GSJ 2012-09-27未认领首款的成单不能认领尾款BEGIN---------
  DECLARE @wid INT
  SELECT @wid= COUNT(1) FROM Order_PaymentRecord WHERE OId=@OrderId AND SWId=1 AND FinanceFlag=0
  IF @wid>0
  BEGIN
  	RETURN 
  END
  --------GSJ 2012-09-27未认领首款的成单不能认领尾款END---------------
  --
   --By GSJ 2012-08-14 将16:30后的数据调整到第2天 Begin--
	Declare @nDays int,@nHour int,@nMinute int,@dToday DateTime
--	Set @dToday = GETDATE() --初始化日期
	--16:30分调整日期
--	Set @nHour = 16
--	Set @nMinute = 30

	--今天+1天与下月1日进行比较，相同则说明今天是本月最后1天
--	If Month(GetDate())<12
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())) + '-' + Convert(varchar(2),Month(GetDate())+1) + '-1'))
--	Else
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())+1) + '-01-01'))
--	If @nDays>0 --不是最后1天
--	Begin
--		If DatePart(hh,GETDATE())>@nHour Or (DatePart(hh,GETDATE())=@nHour And DatePart(mi,GETDATE())>@nMinute)	--超过16:30分
--			Set @dToday = DateAdd(hh,12,GETDATE())	--加12小时
--	END
	Set @dToday=dbo.Get_SetDateTime()
	--By GSJ 2012-08-14 将16:30后的数据调整到第2天 End --             

    DECLARE @sqlstr VARCHAR(1000)
		--PRINT @Id
	/* 修改Order_PaymentRecord表中的RID（[Finance_RunningAccount]中的ID） */
	SET @sqlstr ='UPDATE Order_PaymentRecord 
	SET
	Order_PaymentRecord.Rid=frac.RId,
	Order_PaymentRecord.Prices=frac.Income,
	Order_PaymentRecord.FinanceFlag=1,
	Order_PaymentRecord.FinanceDate='''+CONVERT(varchar, @dToday, 120 )+'''
	FROM ( select sum(Income) as Income,max(RId) as RId from  Finance_RunningAccountCopy  where id IN ('+@Id+') ) AS frac 
	WHERE Order_PaymentRecord.Id='+Convert(varchar,@PayId)+' ' --frac.id=@Id
	/* 修改Order_PaymentRecord表中的RID（[Finance_RunningAccount]中的ID）END */
	EXEC (@sqlstr)
	--PRINT (@sqlstr)
	if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		end
	--PRINT @sqlstr   
	                             --
	/* 修改[Finance_RunningAccount]表中的Renling（认领状态），改为已认领,OrderId(记单号)  */
	SET @sqlstr ='UPDATE [Finance_RunningAccount] SET Renling = 2,RenlingTime='''+CONVERT(varchar,@dToday,120)+''',OrderId = '''+@OrderId+''',isGreen=1 WHERE Finance_RunningAccount.id in (SELECT  Finance_RunningAccountCopy.RId from Finance_RunningAccountCopy WHERE Finance_RunningAccountCopy.id IN ('+@Id+') )'
	/* 修改[Finance_RunningAccount]表中的Renling（认领状态），改为已认领,OrderId(记单号) END */
	EXEC (@sqlstr)
	

		if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		end
	 	PRINT @sqlstr
	
	--修改订单表 FinanceRenlingDate=GetDate(),
	SET @sqlstr ='Update [Order] Set 		
		ShiShouPrice=(Select Sum(Prices) From Order_PaymentRecord Where OId=[Order].OId and FinanceFlag=1),
		
		flag=0
	Where OId='''+@OrderId +''''


	EXEC (@sqlstr)
		
		if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		end
	PRINT @sqlstr 
	
	--删除中间数据
	SET @sqlstr ='Delete From Finance_RunningAccountCopy Where  Id IN ('+@Id+')' --Id=@Id	
	EXEC (@sqlstr)
	
		if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		end
	PRINT @sqlstr 
	 
	--不再添加新的Order_PaymentRecord记录，所以不用将老记录删除 
	--Delete From Order_PaymentRecord Where Id=@PayId
	
	--IF @errorSun<>0
	--BEGIN
	--	PRINT '有错误，回滚'
	--	ROLLBACK TRANSACTION--事务回滚语句
	--END
	--ELSE
	--	BEGIN
	--	PRINT '成功，提交'
	--	COMMIT TRANSACTION--事务提交语句
	--	END                  
   

 e:
    PRINT @errorSun
	if @errorSun>0
	begin
		rollback TRANSACTION
	end
	else
	begin
		commit TRANSACTION
	END
	
	                  
end
	RETURN